package com.offcn.dao;

import com.offcn.utils.PageTool;
import com.offcn.entity.User;

import java.util.List;

public class UserDao extends BaseDao<User> {
    private BaseDao baseDao = new BaseDao();
    public User adminLongin(User user) {

        return (User) baseDao.singleline("select * from user where username=? and password = ? and status = 1 and role = 1 ", User.class, user.getUsername(), user.getPassword());

    }

    public List<User> findAllUser(PageTool pageTool, String search) {
        String sql ="select * from user";
        if (search!=null){
            sql+=" where name like '%"+search+"%'";
        }
        sql+=" limit ?, ?";
    return baseDao.moreline(sql,User.class,pageTool.getStartIndex(),pageTool.getPageSize());
    }

    public int findAllCount(String search) {
            String sql ="select count(*) from user";
        if (search!=null){
        sql+=" where name like '%"+search+"%'";
        }
        long aLong = (long) baseDao.singLeData(sql);
        return (int) aLong;
    }

    public int addUser(User user) {
   return baseDao.dml("insert into user values (null,?,?,?,?,?,?,?,now(),?,null) ",
            user.getName(),user.getPhone(),user.getAge(),user.getSex(),
            user.getUsername(),user.getPassword(),user.getStatus(),user.getRole());
    }

    public int delectUser(String uids) {
        return baseDao.dml("delete from user where uid in ("+uids+")");
    }

    public int updateUser(User user) {
      return   baseDao.dml("update user set name= ? , phone = ?, age = ? ,sex = ? ,username = ? " +
                        ", password = ? , status = ? ,role = ? where uid = ?",
                user.getName(),user.getPhone(),user.getAge(),user.getSex(),
                user.getUsername(),user.getPassword(),user.getStatus(),user.getRole(),user.getUid());
    }

    public User findUserById(String uid) {
        return singleline("select * from user where uid = ?", User.class,uid);
    }
}
